/*
	flad : Copyright 2011 Dale J Williams

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package lads.flad.scene.collision {

	public class CollisionBox extends CollisionPoint {

		private var _height:Number;
		private var _width:Number;
		
		private var _top:CollisionPoint;
		private var _left:CollisionPoint;
		private var _bottom:CollisionPoint;
		private var _right:CollisionPoint;
		
		private var _anchorX:Number = 0;
		private var _anchorY:Number = 0;
		
		public function CollisionBox(width:Number=0, height:Number=0) {
			_top = new CollisionPoint();
			_left = new CollisionPoint();
			_bottom = new CollisionPoint();
			_right = new CollisionPoint();
			this.width = width;
			this.height = height;
		}
		
		public function updateCollision(target:ICollidable, x:Number=NaN, y:Number=NaN):void {
			if (!isNaN(x)) this.x = x;
			if (!isNaN(y)) this.y = y;			
			top.hit = target.isCollision(x + top.x, y + top.y);
			bottom.hit = target.isCollision(x + bottom.x, y + bottom.y);
			left.hit = target.isCollision(x + left.x, y + left.y);
			right.hit = target.isCollision(x + right.x, y + right.y);
		}
		
		private function updatePoints():void {
			_left.x = _width * -0.5 + _anchorX;
			_right.x = _width * 0.5 + _anchorX;
			_left.y = _right.y = _anchorY;			
			_top.y = _height * -0.5 + _anchorY;
			_bottom.y = _height * 0.5 + _anchorY;
			_top.x = _bottom.x = _anchorX;
		}
		
		public function get width():Number { return _width; }		
		public function set width(value:Number):void {
			_width = value;
			updatePoints();
		}
		
		public function get height():Number { return _height; }		
		public function set height(value:Number):void {
			_height = value;
			updatePoints();
		}
		
		public function get anchorX():Number { return _anchorX; }		
		public function set anchorX(value:Number):void {
			_anchorX = value;
			updatePoints();
		}
		
		public function get anchorY():Number { return _anchorY; }		
		public function set anchorY(value:Number):void {
			_anchorY = value;
			updatePoints();
		}
		
		override public function get hit():Boolean { 
			return _top.hit || _left.hit || _bottom.hit || _right.hit; 
		}		
		
		public function get top():CollisionPoint { return _top; }
		
		public function get left():CollisionPoint { return _left; }
		
		public function get bottom():CollisionPoint { return _bottom; }
		
		public function get right():CollisionPoint { return _right; }
		
	}

}